﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpElementCanBePickedByCategoryInModifyAction.h"
#include "IElement.h"
#include "IElementBasicInformation.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

GbmpElementCanBePickedByCategoryInModifyAction::GbmpElementCanBePickedByCategoryInModifyAction()
    : m_isEnabled(true)
{
}

bool gcmp::GbmpElementCanBePickedByCategoryInModifyAction::CanBePicked(const IElement * pElement) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"输入的Element指针为空，不合法",L"GDMPLab",L"2024-03-30");
    const IElementBasicInformation * pElementBasicInformation = pElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElementBasicInformation, L"输入的pElementBasicInformation指针为空，不合法",L"GDMPLab",L"2024-03-30");
    const UniIdentity & category = pElementBasicInformation->GetCategoryUid();
    return m_categoriesCanNotBePicked.find(category) == m_categoriesCanNotBePicked.end();
}

gcmp::OwnerPtr<gcmp::IElementCanBePickedInModifyAction> gcmp::GbmpElementCanBePickedByCategoryInModifyAction::Clone() const
{
    return NEW_AS_OWNER_PTR(GbmpElementCanBePickedByCategoryInModifyAction, *this);
}

bool gcmp::GbmpElementCanBePickedByCategoryInModifyAction::IsEnabled() const
{
    return m_isEnabled; 
}

bool gcmp::GbmpElementCanBePickedByCategoryInModifyAction::SetIsEnabled(bool isEnabled)
{
    m_isEnabled = isEnabled; 
    return true;
}

void gcmp::GbmpElementCanBePickedByCategoryInModifyAction::InitCategories(const std::vector<gcmp::UniIdentity>& categoriesIds)
{
    m_categoriesCanBePicked.clear();
    m_categoriesCanNotBePicked.clear();
    FOR_EACH(id, categoriesIds)
    {
        m_categoriesCanBePicked.insert(id);
    }
}

void gcmp::GbmpElementCanBePickedByCategoryInModifyAction::ClearCategories(const std::vector<gcmp::UniIdentity>& categoriesIds)
{
    m_categoriesCanBePicked.clear();
    m_categoriesCanNotBePicked.clear();
    FOR_EACH(id, categoriesIds)
    {
        m_categoriesCanNotBePicked.insert(id);
    }
}

void gcmp::GbmpElementCanBePickedByCategoryInModifyAction::SetCanBePickedCategory(const gcmp::UniIdentity & uid)
{
    if (uid.IsValid())
    {
        //如果没有，加上
        auto itor1 = m_categoriesCanBePicked.find(uid);
        if (itor1 == m_categoriesCanBePicked.end())
        {
            m_categoriesCanBePicked.insert(uid);
        }

        //如果有，移除
        auto itor2 = m_categoriesCanNotBePicked.find(uid);
        if (itor2 != m_categoriesCanNotBePicked.end())
        {
            m_categoriesCanNotBePicked.erase(itor2->GetGuid());
        }
    }
}

void gcmp::GbmpElementCanBePickedByCategoryInModifyAction::SetCanNotBePickedCategory(const gcmp::UniIdentity & uid)
{
    if (uid.IsValid())
    {
        //如果有，移除
        auto iter1 = m_categoriesCanBePicked.find(uid);
        if (iter1 != m_categoriesCanBePicked.end())
        {
            m_categoriesCanBePicked.erase(iter1->GetGuid());
        }

        //如果没有，加上
        auto iter2 = m_categoriesCanNotBePicked.find(uid);
        if (iter2 == m_categoriesCanNotBePicked.end())
        {
            m_categoriesCanNotBePicked.insert(uid);
        }
    }
}
